﻿/*--------------------------------------------------------------------\
|       _____ _                      __  __ _    _  _____ _    _      |
|      / ____| |                    |  \/  | |  | |/ ____| |  | |     |
|     | (___ | |__   __ _ _ __ _ __ | \  / | |  | | (___ | |__| |     |
|      \___ \| '_ \ / _` | '__| '_ \| |\/| | |  | |\___ \|  __  |     |
|      ____) | | | | (_| | |  | |_) | |  | | |__| |____) | |  | |     |
|     |_____/|_| |_|\__,_|_|  | .__/|_|  |_|\____/|_____/|_|  |_|     |
|                             | |                                     |
|                             |_| Copyright(C) Shawn Sagady 2010      |
|=-------------------------------------------------------------------=|
|   Please reference the CREDITS.TXT file for license and credits.    |
\--------------------------------------------------------------------*/


using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Threading;

namespace SharpMUSH
{
    public class Connection
    {
        static object BigLock = new object();
        Socket socket;
        private StreamReader Reader;
        private StreamWriter Writer;

        static ArrayList connections = new ArrayList();

        public Connection(Socket socket)
        {
            this.socket = socket;
            
            Reader = new StreamReader(new NetworkStream(socket, false));
            Writer = new StreamWriter(new NetworkStream(socket, true));
            new Thread(ClientLoop).Start();
        }

        void ClientLoop()
        {
            try
            {
                lock (BigLock)
                {
                    OnConnect();
                }

                while (true)
                {
                    lock (BigLock)
                    {
                        foreach (Connection conn in connections)
                        {
                            conn.Writer.Flush();
                        }
                    }

                    string line = Reader.ReadLine();

                    if (line == null)
                    {
                        break;
                    }

                    lock (BigLock)
                    {
                        ProcessLine(line);
                    }
                }
            }
            finally
            {
                lock (BigLock)
                {
                    OnDisconnect();
                    socket.Close();
                    
                }
            }
        }

        void OnConnect()
        {
            Console.WriteLine("CONNECTION: New client connected from {0}", socket.RemoteEndPoint.ToString());

            Writer.WriteLine("Welcome!");
            connections.Add(this);
        }

        void OnDisconnect()
        {
            Console.WriteLine("CONNECTION: Client has disconnected from {0}", socket.RemoteEndPoint.ToString());

            connections.Remove(this);
        }

        void ProcessLine(string line)
        {
            foreach (Connection conn in connections)
            {
                conn.Writer.WriteLine("Someone says, '" + line.Trim() + "'");

            }
        }

        public void Print(string line, params object[] list)
        {
            Writer.Write(FormatString(line), list);
        }

        public void Print(string line)
        {
            Writer.Write(FormatString(line));
        }

        public void PrintLine(string line, params object[] list)
        {
            Writer.WriteLine(FormatString(line), list);
        }

        public void PrintLine(string line)
        {
            Writer.WriteLine(FormatString(line));
        }

        private string FormatString(string line)
        {
            // Do some stuff here and return the string

            // strip or add ansi as needed

            return line;
        }
    }
}
